;(function () {
'use strict';
angular.module('services.faultLocation.appAlarmServer')
  .factory('publicServe', ['$mdDialog', '$filter', function($mdDialog, $filter) {
    return {
      commonality: function() {
        var startDate = new Date();
        startDate.setDate(startDate.getDate() - 30);
        var endDate = new Date();
        var obj = {
          isStrain: '01',
          systemCode: '',
          alarmLevelCode: [],
          timeGra: '1',
          startTime: startDate,
          endTime: endDate,
          alarmStateCode: [],
          msg: ''
        };
        return obj;
      },
      /**
       *@提示信息
       *@param{content} 提示的主体信息
       *@export{}
       */
      popup: function(content) {
        $mdDialog.show(
          $mdDialog.alert().title('提示框')
          .content(content)
          .ok('确定')
        );
      },
      /**
       *@弹出提示框，并将开始时间和结束时间以格式化成特定的格式
       *@param{obj}
       *@export{}
       */
      prompt: function(obj) {
        var message = '';
        if (obj.systemCode === null || obj.systemCode === '') {
          message = 'system';
          this.popup('请选择系统名称!');
          return;
        }
        if (obj.startTime === null || obj.startTime === '') {
          message = 'startTime';
          this.popup('请选择开始时间!');
          return;
        } else {
          if (obj.timeGra === '1') {
            obj.startTime = $filter('date')(obj.startTime, 'yyyy-MM-dd') + ' 00:00:00';
          } else {
            obj.startTime = $filter('date')(obj.startTime, 'yyyy-MM-dd HH') + ':00:00';
          }
        }
        if (obj.endTime === null || obj.endTime === '') {
          message = 'endTime';
          this.popup('请选择结束时间!');
          return;
        } else {
          if (obj.timeGra === '1') {
            obj.endTime = $filter('date')(obj.endTime.setDate(obj.endTime.getDate() + 1), 'yyyy-MM-dd') + ' 00:00:00';
          } else {
            obj.endTime = $filter('date')(obj.endTime.setHours(obj.endTime.getHours() + 1), 'yyyy-MM-dd HH') + ':00:00';
          }
        }
        return message;
      },
      /**
       *@将时间以特定的格式在页面上显示
       *@param{obj，array} obj:对象  array:数组
       *@export{}
       */
      getTimeFormat: function(obj, array) {

        angular.forEach(array, function(item) {
          if (obj.timeGra === '1') {
            var dayTime = new Date(item.time);
            item.startTime = $filter('date')(dayTime.setDate(dayTime.getDate()), 'yyyy-MM-dd') + ' 00:00:00';
            item.endTime = $filter('date')(dayTime.setDate(dayTime.getDate() + 1), 'yyyy-MM-dd') + ' 00:00:00';
          } else {
            var year = item.time.slice(0, 4);
            var month = item.time.slice(5, 7);
            var day = item.time.slice(8, 10);
            var hour = item.time.slice(11, 13);
            var hourDate = new Date(year, month - 1, day, hour);
            item.startTime = $filter('date')(hourDate.setHours(hourDate.getHours()), 'yyyy-MM-dd HH') + ':00:00';
            item.endTime = $filter('date')(hourDate.setHours(hourDate.getHours() + 1), 'yyyy-MM-dd HH') + ':00:00';
          }
        });
      },
      /*
       *@param timeFmt {function} 根据选择不同的帐期类型，计算从结束时间到到开始时间的差，
       *放入到timeArr数组中，并且以timeArr为echarts图的x轴值
       *@param availableObj {obj} 传入的对象，里面必须包含availableObj.timeType,
       *availableObj.startTime,availableObj.endTime三个属性，分别为所选择的时间账期，开始时间，结束时间。
       */
      calculateDDValue: function(availableObj, timeArr) {
        
        var tType = availableObj.timeType;
        var start = availableObj.startTime.getTime();
        var end = availableObj.endTime.getTime();
        var startYear = availableObj.startTime.getFullYear();
        var endYear = availableObj.endTime.getFullYear();
        switch (tType) {
          case '4': //年
            var sumYear = endYear - startYear;
            timeArr.push(startYear + '年');
            for (var b = 0; b < sumYear; b++) {
              startYear = startYear + 1;
              timeArr.push(startYear + '年');
            }
            break;
          case '3': //季
            var startQuarter = Number(availableObj.startQuarter);
            var endQuarter = Number(availableObj.endQuarter);
            var sumQuarter = (endYear * 4 + endQuarter) - (startYear * 4 + startQuarter);
            timeArr.push(startYear + '年第' + startQuarter + '季度');
            for (var e = 0; e < sumQuarter; e++) {
              if (startQuarter >= 4) {
                startQuarter = 1;
                startYear += 1;
                timeArr.push(startYear + '年第' + startQuarter + '季度');
              } else {
                startQuarter += 1;
                timeArr.push(startYear + '年第' + startQuarter + '季度');
              }
            }
            break;
          case '2': //月
            var startMonth = availableObj.startTime.getMonth() + 1;
            var endMonth = availableObj.endTime.getMonth() + 1;
            var sumMonth = (endYear * 12 + endMonth) - (startYear * 12 + startMonth);
            timeArr.push($filter('date')(new Date(start), 'yyyy年MM月'));
            var xDate = $filter('date')(new Date(start), 'yyyy年MM月');
            var year = Number(xDate.substring(0, 4));
            var month = Number(xDate.substring(5, 7));
            for (var d = 0; d < sumMonth; d++) {
              if (month >= 12) {
                month = 1;
                year += 1;
                timeArr.push(year + '年0' + month + '月');
              } else if (month < 12 && month >= 9) {
                month += 1;
                timeArr.push(year + '年' + month + '月');
              } else {
                month += 1;
                timeArr.push(year + '年0' + month + '月');
              }
            }
            break;
          case '0': //天
            var sumDay = Math.floor((end - start) / (24 * 60 * 60 * 1000));
            timeArr.push($filter('date')(new Date(start), 'yyyy年MM月dd日'));
            for (var a = 0; a < sumDay; a++) {
              start = start + (24 * 60 * 60 * 1000);
              var yDate = new Date(start);
              yDate = $filter('date')(yDate, 'yyyy年MM月dd日');
              timeArr.push(yDate);
            }
            break;
          case '6': //时
            var sumHour = Math.floor((end - start) / (60 * 60 * 1000));
            timeArr.push($filter('date')(new Date(start), 'yyyy年MM月dd天 HH时'));
            for (var c = 0; c < sumHour; c++) {
              start = start + (60 * 60 * 1000);
              var zDate = new Date(start);
              zDate = $filter('date')(zDate, 'yyyy年MM月dd天 HH时');
              timeArr.push(zDate);
            }
            break;
        }
      },
      getTimeGra: function() {
        var timeGra = [];
        timeGra = [{
          name: '天',
          code: '1'
        }, {
          name: '小时',
          code: '0'
        }];
        return timeGra;
      },
      timeDiff: function(availableObj) {
        var timeArr = [];
        var start = new Date(availableObj.startTime).getTime();
        var end = new Date(availableObj.endTime).getTime();
        if (availableObj.timeGra === '1') {
          var sumDay = Math.floor((end - start) / (24 * 60 * 60 * 1000));
          timeArr.push($filter('date')(new Date(start), 'yyyy-MM-dd'));
          for (var i = 0; i < sumDay; i++) {
            start = start + (24 * 60 * 60 * 1000);
            var d = new Date(start); 
            d = $filter('date')(d, 'yyyy-MM-dd');
            timeArr.push(d);
          }
        } else {
          var sumHour = Math.floor((end - start) / (60 * 60 * 1000));
          timeArr.push($filter('date')(new Date(start), 'yyyy-MM-dd HH'));
          for (var h = 0; h < sumHour; h++) {
            start += (60 * 60 * 1000);
            var f = new Date(start);
            f = $filter('date')(f, 'yyyy-MM-dd HH');
            timeArr.push(f);
          }
        }
        return timeArr;
      }
    };
  }]);
})();